package com.grupoconstruobras.sigo.general.interceptores;

import com.grupoconstruobras.sigo.general.ui.converter.UpperCaseConverter;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

public class ProcessValidationsPhaseListener implements PhaseListener {

  /**
	 * 
	 */
  private static final long serialVersionUID = -8871632615118026364L;

  public PhaseId getPhaseId() {
    return PhaseId.PROCESS_VALIDATIONS;
  }

  public void beforePhase(PhaseEvent event) {
	  processViewTree(event.getFacesContext().getViewRoot());
  }

  public void afterPhase(PhaseEvent event) {
    
  }
  
  private void processViewTree(UIComponent component) {
	  if(component instanceof UIInput) {
		setUpperCaseConverter((UIInput) component);  
	  }
	  
	  List<UIComponent> children = component.getChildren();
	  if(children != null && !children.isEmpty()) {
		  for(UIComponent child : children)
			  processViewTree(child);
	  }
  }
  
  private void setUpperCaseConverter(UIInput input) {
	  input.setConverter(new UpperCaseConverter());
  }

}